package com.umeox.iqibla.custom_tasbih.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.iqibla.custom_tasbih.adapter.CustomTasbihRankingAdapter
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.apiclient.model.ChantsCounterDTO
import com.umeox.watch.moto.apiclient.model.FriendChantsCounterDTO
import com.umeox.watch.moto.dataservice.DataProvider
import com.umeox.watch.moto.dataservice.proxy.PushApiClientProxy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class TaskRankingActivity : ThemeAppCompatActivity() {

    private lateinit var adapter: CustomTasbihRankingAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_task_ranking)
        val rv = findViewById<RecyclerView>(R.id.rv_ranking)
        val linearLayoutManager = LinearLayoutManager(mContext)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        rv.layoutManager = linearLayoutManager
        adapter = CustomTasbihRankingAdapter()
        rv.adapter = adapter

        val tempArray = mutableListOf<FriendChantsCounterDTO>()
        tempArray.add(getSelfData())
        adapter.setFriendChantsCounterDTOList(tempArray)
        getData()
    }

    private fun getData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val tempArray = mutableListOf<FriendChantsCounterDTO>()
            try {
                val result: ChantsCounterDTO = PushApiClientProxy.fetchBinder().chantsCounter
                tempArray.addAll(result.friendsTotal)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            withContext(Dispatchers.Main) {
                fixFriendStepOTDList(tempArray)
                if (tempArray.size == 1) {
                    findViewById<TextView>(R.id.tv_desc).visibility = View.VISIBLE
                } else {
                    findViewById<TextView>(R.id.tv_desc).visibility = View.GONE
                }
                adapter.setFriendChantsCounterDTOList(tempArray)
            }
        }
    }

    @SuppressLint("NewApi")
    private fun fixFriendStepOTDList(tempArray: MutableList<FriendChantsCounterDTO>) {
        //添加自己的数据
        tempArray.add(getSelfData())
        tempArray.sortByDescending {
            it.amount
        }
    }

    private fun getSelfData() : FriendChantsCounterDTO {
        val selfChantsCounterDTO = FriendChantsCounterDTO()
        selfChantsCounterDTO.memberId = DataProvider.getHolderId()
        selfChantsCounterDTO.type = -1
        selfChantsCounterDTO.name = DataProvider.getNickname()
        selfChantsCounterDTO.avatar = DataProvider.getAvatar()
        selfChantsCounterDTO.avatarIndex = -1
        selfChantsCounterDTO.createTime = System.currentTimeMillis()
        selfChantsCounterDTO.amount = DataProvider.getChantsCounterChallengeTotal() + DataProvider.getChantsCounterTotal()
        return selfChantsCounterDTO
    }

}